home *** CD-ROM | disk | FTP | other *** search
- /* MenuMaze.c by David Oster and Matthew Grayson
- * June 6, 1987
- * Synopsis: This program is an example of how to write hierarchical menus
- * and why you should't. Hierarchical menus are a feature in the ROMs of the
- * Macintosh SE and the Mac II. They are available to mere mortals who are
- * using Apple's System 4.1 system software and who have the 128K ROMs:
- * People who have either a MacPlus or a 512K Mac, Enhanced.
- *
- * This program compiles under LightSpeed C version 2.01.
- * This program may be freely redistributed. It is hereby placed in the
- * public domain. So There.
- */
-
- #include "ControlMgr.h"
- #include "DialogMgr.h"
- #include "EventMgr.h"
- #include "MacTypes.h"
- #include "MemoryMgr.h"
- #include "MenuMgr.h"
- #include "QuickDraw.h"
- #include "StdFilePkg.h"
- #include "TextEdit.h"
- #include "WindowMgr.h"
- #include "OSUtil.h"
-
- #define NOT !
-
- #define NIL 0L
-
- #define Integer int
- typedef long int LongInt;
-
- /* some 128k Rom mac defines */
- #define OldRomBit 0x8000
- extern int RomVersion : 0x28E;
- extern int MenuHeight : 0xBAA;
-
- /* trap numbers of some new traps */
- #define UNIMPL 0x9F
- #define NEWTE 0x3D
-
-
-
- enum {
- APPLEMENU = 1,
- FILEMENU,
- EDITMENU
- };
-
- typedef EventRecord *EventPtr;
- #define ABOUTI 1 /* About Grid Pic… */
-
-
- enum {
- rABOUT = 131
- };
-
- /* globals: contains screen in grid form */
- WindowPtr myWindow;
- Ptr buffer;
-
- void GoMouseDown(), GoKey(), GoMenuBar(), GoCommand(), DoAppleMenu(),
- DoDeskAcc(), DoFileMenu(), DoEditMenu(), DoExitMenu();
-
-
- void DoQuit(){
- ExitToShell();
- }
-
- void NoGo(alertId)Integer alertId;{
- StopAlert(alertId, NIL);
- DoQuit();
- }
-
- main(){
- void Init(), OneEvent(), GoIdle();
-
- Init();
- while(TRUE){
- OneEvent();
- SystemTask();
- GoIdle();
- }
- }
-
- /* OneEvent - dispatch on events, do idle processing
- */
-
- void OneEvent(){
- void DrawBar(), HideBar();
- EventRecord myEvent;
-
- if(GetNextEvent(everyEvent, &myEvent)){
- switch(myEvent.what){
- case updateEvt: GoUpdateEvent( &myEvent);
- break;
- case mouseDown: GoMouseDown( &myEvent);
- break;
- }
- }
- }
-
- /* GoUpdateEvent - update that window
- */
- GoUpdateEvent(theEvent)EventPtr theEvent;{
- Ptr bufP; /* point into picture buffer */
- GrafPtr savePtr;
-
- if(myWindow == (WindowPtr) theEvent->message){
- GetPort(&savePtr);
- SetPort( (WindowPtr) theEvent->message);
- BeginUpdate(thePort);
- EndUpdate(thePort);
- SetPort(savePtr);
- }
- }
-
- /* GoMouseDown - handle mouse down from event manager
- */
-
- void GoMouseDown(theEvent)EventPtr theEvent;{
- void GoContent();
- WindowPtr whichWindow;
-
- switch(FindWindow(theEvent->where, &whichWindow)){
- case inMenuBar : GoMenuBar(theEvent);
- break;
- case inSysWindow : SystemClick(theEvent, whichWindow);
- break;
- case inContent : GoContent(theEvent);
- break;
- }
- }
-
- /* Init - Start things off
- *
- * We init managers we don't need, (Maybe a desk accessory will)
- */
-
- void Init(){
- Integer i;
-
- FlushEvents(everyEvent, 0);
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitCursor();
- InitDialogs(DoQuit);
- InitMenus();
- TEInit();
- SetMenuBar(GetNewMBar(128));
- AddResMenu(GetMHandle(APPLEMENU), 'DRVR');
- if(RomVersion & OldRomBit)
- NoGo(128);
- if(NGetTrapAddress(UNIMPL, ToolTrap) == NGetTrapAddress(NEWTE, ToolTrap))
- NoGo(129);
- for(i = 19;GetResource('MENU', i) != NIL;i++){
- InsertMenu(GetMenu(i), -1);
- }
- for(i = 31;GetResource('MENU', i) != NIL;i++){
- InsertMenu(GetMenu(i), -1);
- }
- DrawMenuBar();
- }
-
- void GoContent(theEvent)EventPtr theEvent;{
- Point where;
- where = theEvent->where;
- GlobalToLocal(&where);
- /* ClearMarquee(); */
- /* SetMarquee(where); */
- }
- /* GoIdle - spin that marquee
- */
-
- void GoIdle(){
- /* IdleMarquee(); */
- }
- /* GoMenuBar - handle mousedown in menu bar
- */
- void GoMenuBar(theEvent)EventPtr theEvent;{
- GoCommand(MenuSelect(theEvent->where));
- }
- /* GoCommand - dispatches on the menu.
- * It guarantees that the item in the menubar is
- * hilighted long enough to be able to see it.
- */
- #define QUITI 2
- #define EXITMENU 22
-
- void GoCommand(theCom)LongInt theCom;{
- LongInt endTime, newTime;
- Integer theLow;
- Boolean dontCare;
-
- theLow = LoWord(theCom);
- endTime = 5 + TickCount();
- switch(HiWord(theCom)){
- case APPLEMENU : DoAppleMenu(theLow);
- break;
- case EDITMENU : dontCare = SystemEdit(theLow -1);
- break;
- case EXITMENU : DoExitMenu(theLow);
- }
- newTime = TickCount();
- if(newTime < endTime){ /* let's see that command */
- Delay(endTime - newTime, &newTime);
- }
- HiliteMenu(0);
- }
-
- /* DoAppleMenu - handle mouse down in apple menu
- */
-
- void DoAppleMenu(theItem)Integer theItem;{
- Rect r;
- switch(theItem){
- case ABOUTI:
- theItem = Alert(rABOUT, NIL);
- break;
- default : DoDeskAcc(theItem);
- }
- }
-
-
- /* DoDeskAcc - run a desk accessory
- */
-
- void DoDeskAcc(theItem)Integer theItem;{
- Str255 accessory;
-
- GetItem(GetMHandle(APPLEMENU), theItem, accessory);
- theItem = OpenDeskAcc(accessory); /* don't care about return value */
- }
-
- /* DoFileMenu -
- */
-
- void DoFileMenu(theItem)Integer theItem;{
- void OpenGrid(), OpenPaint(), SavePaint(), SaveGrid(),
- ToPaint(), ToGrid(), DoQuit();
-
- switch(theItem){
- default: DoQuit();
- }
- }
-
- /* DoEditMenu -
- */
-
- void DoExitMenu(theItem)Integer theItem;{
- switch(theItem){
- case QUITI:
- Alert(130, NIL);
- DoQuit();
- }
- }
-
-
-